package com.handy.controller.admin.setting;

import cn.hutool.core.util.DesensitizedUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.handy.entity.log.LogLogin;
import com.handy.service.log.ILogLoginService;
import com.handy.vo.PageBean;
import com.handy.vo.PageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 登录日志接口
 *
 * @author handy
 */
@RestController
@RequestMapping("/api/admin/setting/logLogin")
@Api(tags = "登录日志")
@ApiSupport(author = "handy")
@RequiredArgsConstructor
public class LogLoginApiController {
    private final ILogLoginService logLoginService;

    @GetMapping("/list")
    @ApiOperation(value = "获取列表数据")
    public PageVo<LogLogin> list(PageBean pageBean) {
        LambdaQueryWrapper<LogLogin> wrapper = Wrappers.lambdaQuery(new LogLogin()).orderByDesc(LogLogin::getCreateTime);
        IPage<LogLogin> logLoginPage = logLoginService.page(new Page<>(pageBean.getPage(), pageBean.getLimit()), wrapper);
        // 进行关键信息脱敏
        List<LogLogin> records = logLoginPage.getRecords();
        for (LogLogin record : records) {
            record.setCode(DesensitizedUtil.mobilePhone(record.getCode()));
        }
        return PageVo.create(records, logLoginPage.getTotal());
    }

}